﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for ChatRoomMembersDA
/// </summary>
public class ChatRoomMembersDA
{
	public ChatRoomMembersDA()
	{
		
	}

    public int Join(ChatRoom room, String userID)
    {
        string command = "sp_tb_chatroom_members_JoinRoom";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@roomID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@roomID"].Value = room.ID;
        sqlCmd.Parameters["@userID"].Value = userID;
        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }

    public int Leave(ChatRoom room, String userID)
    {
        string command = "sp_tb_chatroom_members_LeaveRoom";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@roomID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@roomID"].Value = room.ID;
        sqlCmd.Parameters["@userID"].Value = userID;
        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }
}
